/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using System.Drawing.Drawing2D;

namespace MyPhotoIndex.Controls
{
    public partial class WizardHost : FadingForm
    {
        protected event EventHandler<PageNumberChangedEventArgs> PageNumberChangedEvent;
        protected event EventHandler WizardFinishedEvent;
        private Dictionary<int, WizardPageInfo> m_pageInfo = new Dictionary<int,WizardPageInfo>();

        private WizardPageInfo m_defaultPageInfo;
        private int m_pageNumber;
        private int m_numOfPages = 1;

        public WizardHost()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;

            m_defaultPageInfo = new WizardPageInfo();
            m_defaultPageInfo.NextButtonText = NextButton.Text;

            m_numOfPages = 1;
            BackButton.Enabled = false;
            panel2.Paint += new PaintEventHandler(panel2_Paint);
            panel1.Paint += new PaintEventHandler(panel1_Paint);
            WizardPicture.Paint += new PaintEventHandler(WizardPicture_Paint);

            base.Load += new EventHandler(WizardHost_Load);
        }

        void WizardHost_Load(object sender, EventArgs e)
        {
            PageNumber = 0;
            SetPageNextButtonText(m_numOfPages - 1, Resources.Resources.Wizard_Finish);
        }

        [EditorBrowsable(EditorBrowsableState.Always),
        Browsable(true),
        DefaultValue(0),
        Category("Wizard host"),
        Description("current page number")]
        public int PageNumber
        {
            get { return m_pageNumber; }
            set 
            { 
                m_pageNumber = value;
                UpdateDisplay();
            }
        }

        [EditorBrowsable(EditorBrowsableState.Always), 
        Browsable(true), 
        DefaultValue(1), 
        Category("Wizard host"),
        Description("Number of wizard pages")]
        public int NumberOfPages
        {
            get { return m_numOfPages; }
            set { m_numOfPages = value; }
        }

        protected virtual void CancelButtonClick(object sender, EventArgs e)
        {
            this.Close();
        }

        protected virtual void NextButtonClick(object sender, EventArgs e)
        {
            m_pageNumber++;
            BackButton.Enabled = (m_pageNumber > 0);

            if (m_pageNumber == m_numOfPages - 1)
            {
                BackButton.Enabled = false;
                CloseButton.Enabled = false;
            }

            if (m_pageNumber == m_numOfPages)
            {
                DisableButtons();
                if (WizardFinishedEvent != null)
                {
                    WizardFinishedEvent.Invoke(this, EventArgs.Empty);
                }

                this.Close();
                return;
            }

            SetPageInfo();

            UpdateDisplay();

            if (PageNumberChangedEvent != null)
            {
                PageNumberChangedEvent.Invoke(this, new PageNumberChangedEventArgs(m_pageNumber, m_pageNumber - 1));
            }
        }

        protected virtual void BackButtonClick(object sender, EventArgs e)
        {
            m_pageNumber--;
            NextButton.Enabled = (m_pageNumber < (m_numOfPages - 1));
            BackButton.Enabled = (m_pageNumber > 0);

            SetPageInfo();
            UpdateDisplay();

            if (PageNumberChangedEvent != null)
            {
                PageNumberChangedEvent.Invoke(this, new PageNumberChangedEventArgs(m_pageNumber, m_pageNumber + 1));
            }
        }

        protected void DisableButtons()
        {
            NextButton.Enabled = false;
            BackButton.Enabled = false;
            CloseButton.Enabled = false;
        }

        protected void EnableButtons()
        {
            NextButton.Enabled = true;
            BackButton.Enabled = true;
            CloseButton.Enabled = true;
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            pevent.Graphics.Clear(Color.White);
        }

        void panel2_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;

            Rectangle gradDrawingRect = panel2.ClientRectangle;
            Rectangle drawingrect = panel2.ClientRectangle;

            gradDrawingRect.Y += 1;
            drawingrect.Y += 1;
            gradDrawingRect.Height -= 2;

            if (gradDrawingRect.Height <= 0 ||
                gradDrawingRect.Width <= 0)
            {
                return;
            }

            using (Brush parentBrush = new LinearGradientBrush(gradDrawingRect,
            Color.FromArgb(222, 236, 253), Color.FromArgb(171, 191, 216), LinearGradientMode.Vertical))
            {
                graphics.FillRectangle(parentBrush, gradDrawingRect);
            }

            using (Pen pen = new Pen(Color.FromArgb(108, 131, 158)))
            {
                Point start = new Point(0, drawingrect.Height - 2);
                Point end = new Point(drawingrect.Width - 1, drawingrect.Height - 2);
                graphics.DrawLine(pen, start, end);
            }

            using (Brush gradBrush = new LinearGradientBrush(gradDrawingRect,
                Color.FromArgb(222, 236, 253), Color.FromArgb(108, 131, 158), LinearGradientMode.Vertical))
            {
                using (Pen pen = new Pen(gradBrush))
                {
                    Point start = new Point(0, 0);
                    Point end = new Point(0, drawingrect.Height - 1);
                    graphics.DrawLine(pen, start, end);

                    start = new Point(drawingrect.Width - 2, 0);
                    end = new Point(drawingrect.Width - 2, drawingrect.Height - 1);
                    graphics.DrawLine(pen, start, end);
                }
            }

            using (Pen pen = new Pen(Color.FromArgb(79, 94, 113)))
            {
                Point start = new Point(0, drawingrect.Height - 1);
                Point end = new Point(drawingrect.Width, drawingrect.Height - 1);
                graphics.DrawLine(pen, start, end);

                start = new Point(drawingrect.Width - 1, 0);
                end = new Point(drawingrect.Width - 1, drawingrect.Height - 1);
                graphics.DrawLine(pen, start, end);
            }

            using (Pen pen = new Pen(Color.FromArgb(212, 208, 200)))
            {
                Point start = new Point(0, drawingrect.Top - 1);
                Point end = new Point(drawingrect.Width, drawingrect.Top - 1);
                graphics.DrawLine(pen, start, end);
            }
        }

        void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Rectangle drawingrect = panel1.ClientRectangle;

            using (Pen pen = new Pen(Color.FromArgb(212, 208, 200)))
            {
                Point start = new Point((this.RightToLeft == RightToLeft.Yes) ? 0 : (drawingrect.Width - 1), 0);
                Point end = new Point((this.RightToLeft == RightToLeft.Yes) ? 0 : (drawingrect.Width - 1), drawingrect.Bottom);
                graphics.DrawLine(pen, start, end);
            }
        }

        void WizardPicture_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Rectangle drawingrect = WizardPicture.ClientRectangle;

            using (Pen pen = new Pen(Color.FromArgb(212, 208, 200)))
            {
                Point start = new Point((this.RightToLeft == RightToLeft.Yes) ? 0 : (drawingrect.Width - 1), 0);
                Point end = new Point((this.RightToLeft == RightToLeft.Yes) ? 0 : (drawingrect.Width - 1), drawingrect.Bottom);
                graphics.DrawLine(pen, start, end);
            }
        }

        private void SetPageInfo()
        {
            WizardPageInfo pageInfo = null;
            if (m_pageInfo.TryGetValue(m_pageNumber, out pageInfo) == false)
            {
                pageInfo = m_defaultPageInfo;
            }

            NextButton.Text = pageInfo.NextButtonText;
        }

        private void UpdateDisplay()
        {
            foreach (Control control in base.Controls)
            {
                bool visible = false;
                if (control.Tag != null)
                {
                    int idx = Convert.ToInt32(control.Tag);
                    if (idx == m_pageNumber ||
                        idx == -1)
                    {
                        visible = true;
                    }
                }
                else
                {
                    visible = true;
                }

                control.Visible = visible;
            }
        }

        public void SetPageNextButtonText(int pageNumber, string text)
        {
            WizardPageInfo pageInfo = null;
            if (m_pageInfo.TryGetValue(m_pageNumber, out pageInfo) == false)
            {
                pageInfo = new WizardPageInfo();
                m_pageInfo.Add(pageNumber, pageInfo);
            }

            pageInfo.NextButtonText = text;
        }
    }
}